package functions;

import java.util.ArrayList;

public abstract class Term extends FunctionIF implements Cloneable {
	
	
	
	// Differentiation Methods
	protected FunctionIF returnDerivative(FunctionIF FuncDiff, FunctionIF fx)
	{
		if (willProductTerms(fx))
		{
			// in need of returning a multiple function (product of terms)
			// so will function function as a return value

			ArrayList<FunctionIF> product = new ArrayList<FunctionIF>();
			
			FunctionIF df = diffExponent();
			
			product.add(df); // Differentiating the function (exponent part)
			
			product.add(FuncDiff); // i.e. diff of cos
			
			product.add(fx); // diff of the function inside it

			
			
			Function f = new Function(); // = 3*sin(x^2)*(cos(x^2))* (2*x)
			f.addProductTerm(product);
			return f;
		}
		// the exponent is 1 and the d_poly is constant
		FuncDiff.coff *= fx.coff;
		return FuncDiff;
	}

	/***
	 * this method to check if the differentiation will produce a product terms
	 * 
	 * @param fucntion
	 *            the differentiation of the polynomial inside the function
	 * @return
	 */
	private boolean willProductTerms(FunctionIF fucntion)
	{
		// zero case won't happen as this method won't be called 
		if (expon > 1 || expon < 0 || fucntion instanceof Function)
			return true;
		PolynTerm term = (PolynTerm) fucntion;
		if(term.expon  != 0)
			return  true;
		return false;
	}

	
}
